package com.lanou3g.mynewstest.module.news.jokes;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.beans.NewsJokesBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by dllo on 16/1/21.
 */
public class JokesAdapter extends BaseAdapter {
    private Context context;
    private NewsJokesBeans data;
    private int type;


    private String getImageType(int pos) {
        String img = data.getJokes().get(pos).getImgsrc();
        int last = img.lastIndexOf(".");
        String type = img.substring(last, last + 4);
        return type;
    }

    public JokesAdapter(Context context, NewsJokesBeans data) {
        this.context = context;
        this.data = data;

    }

    @Override
    public int getCount() {
        return data.getJokes().size();
    }

    @Override
    public Object getItem(int position) {
        return data.getJokes().get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        type = getItemViewType(position);
        NormalViewHolder normalViewHolder;
        NoPicViewHolder noPicViewHolder;
        final GifViewHolder gifViewHolder;
        View normalView = null;
        View noPicView = null;
        View gifView = null;
        switch (type) {
            case 3:
                if (normalView == null) {
                    normalView = LayoutInflater.from(context).inflate(R.layout.item_jokes_normal, parent, false);
                    normalViewHolder = new NormalViewHolder(normalView);

                    normalView.setTag(normalViewHolder);
                } else {
                    normalViewHolder = (NormalViewHolder) normalView.getTag();
                }
                normalViewHolder.digest.setText(data.getJokes().get(position).getDigest());
                normalViewHolder.downTimes.setText("" + data.getJokes().get(position).getDownTimes());
                normalViewHolder.upTimes.setText("" + data.getJokes().get(position).getUpTimes());
                MyRequestQueue.setNetImage(normalViewHolder.imageView, data.getJokes().get(position).getImgsrc());
                normalViewHolder.share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showShare();
                    }
                });
                return normalView;
            case 2:
                if (gifView == null) {
                    gifView = LayoutInflater.from(context).inflate(R.layout.item_jokes_gif, parent, false);
                    gifViewHolder = new GifViewHolder(gifView);
                    gifView.setTag(gifViewHolder);
                } else {
                    gifViewHolder = (GifViewHolder) gifView.getTag();
                }
                gifViewHolder.digest.setText(data.getJokes().get(position).getDigest());
                gifViewHolder.downTimes.setText("" + data.getJokes().get(position).getDownTimes());
                gifViewHolder.upTimes.setText("" + data.getJokes().get(position).getUpTimes());

                Glide.with(context)
                        .load(data.getJokes().get(position).getImgsrc())
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(gifViewHolder.imageView);
                gifViewHolder.share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showShare();
                    }
                });
                return gifView;
            case 1:
                if (noPicView == null) {
                    noPicView = LayoutInflater.from(context).inflate(R.layout.item_jokes_nopic, parent, false);
                    noPicViewHolder = new NoPicViewHolder(noPicView);
                    noPicView.setTag(noPicViewHolder);
                } else {
                    noPicViewHolder = (NoPicViewHolder) noPicView.getTag();
                }
                noPicViewHolder.digest.setText(data.getJokes().get(position).getDigest());
                noPicViewHolder.downTimes.setText("" + data.getJokes().get(position).getDownTimes());
                noPicViewHolder.upTimes.setText("" + data.getJokes().get(position).getUpTimes());
                noPicViewHolder.share.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showShare();
                    }
                });
                return noPicView;

        }
        return convertView;
    }



    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle(context.getString(R.string.ssdk_oks_share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(context.getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

// 启动分享GUI
        oks.show(context);
    }
    @Override
    public int getItemViewType(int position) {
        if (null == data.getJokes().get(position).getImgsrc()) {
            return 1;
        } else if (getImageType(position).equals(".gif")) {
            Log.d("JokesAdapter", getImageType(position));
            return 2;
        } else {
            return 3;
        }
    }

    public void addData(NewsJokesBeans beans) {
        this.data=beans;
        notifyDataSetChanged();
    }

    class NormalViewHolder {
        NetworkImageView imageView;
        TextView digest, upTimes, downTimes;
        ImageView share;

        public NormalViewHolder(View view) {
            this.imageView = (NetworkImageView) view.findViewById(R.id.item_jokes_normal_iv);
            this.digest = (TextView) view.findViewById(R.id.item_jokes_normal_digest);
            this.downTimes = (TextView) view.findViewById(R.id.item_jokes_normal_downtimes_tv);
            this.upTimes = (TextView) view.findViewById(R.id.item_jokes_normal_uptimes_tv);
            this.share = (ImageView) view.findViewById(R.id.item_jokes_normal_share);
        }
    }


    private class NoPicViewHolder {
        TextView digest, upTimes, downTimes;
        ImageView share;

        public NoPicViewHolder(View view) {
            this.digest = (TextView) view.findViewById(R.id.item_jokes_digest);
            this.downTimes = (TextView) view.findViewById(R.id.item_jokes_downtimes_tv);
            this.upTimes = (TextView) view.findViewById(R.id.item_jokes_uptimes_tv);
            this.share = (ImageView) view.findViewById(R.id.item_jokes_share);
        }
    }

    private class GifViewHolder {
        TextView digest, upTimes, downTimes;
        ImageView imageView, share;

        public GifViewHolder(View view) {
            this.digest = (TextView) view.findViewById(R.id.item_jokes_gif_digest);
            this.downTimes = (TextView) view.findViewById(R.id.item_jokes_gif_downtimes_tv);
            this.upTimes = (TextView) view.findViewById(R.id.item_jokes_gif_uptimes_tv);
            this.imageView = (ImageView) view.findViewById(R.id.item_jokes_gif_iv);
            this.share = (ImageView) view.findViewById(R.id.item_jokes_gif_share);
        }
    }
}
